home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
pascal
/
picklst.exe
/
DIALOGS1.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1991-07-26
|
13KB
|
472 lines
unit Dialogs1;
{************************************************}
{ }
{ Turbo Pascal 6.0 }
{ Turbo Vision Unit - Dialogs1 }
{ }
{ Containing: }
{ tSelectItem, tSelectCollection, }
{ tPickList, tPickDialog }
{ tTextDialog }
{ }
{************************************************}
{********************************}
{***Programmed by ***}
{***Blake Watson ***}
{***CIS number 70303,373 ***}
{********************************}
interface
uses Objects, Drivers, Dialogs, Views, MsgBox, App,
Objects1;
const
MaxRows = 21;
type
pSelectItem = ^tSelectItem;
tSelectItem = object(tObject)
Name : pString;
Selected: boolean;
constructor Init(S:String);
destructor Done; virtual;
end;
pSelectCollection = ^tSelectCollection;
tSelectCollection = object(tCollection)
Pick: byte;
constructor Init(S:string);
function NameAt(I:Integer): string;
procedure NewItem(S:String); virtual;
function Selected(I:Integer): boolean;
procedure ToggleAt(I:Integer);
procedure DropNotSelected;
function LastSelectedItem: integer;
function NumberSelected: integer;
end;
pPickList = ^tPickList;
tPickList = object(tView)
List : PSelectCollection;
MaxItemLength, Picked, Highlight, NumRows,
NumCols: integer;
constructor Init(r: tRect; MIL, NC, NR: integer; AList: pSelectCollection);
procedure Draw; virtual;
procedure Choose(AnItem: Integer); virtual;
procedure HandleEvent(var Event:tEvent); virtual;
end;
pPickDialog = ^tPickDialog;
tPickDialog = object(tDialog)
constructor Init(AList: pointer; X,Y: Integer);
procedure GetDims(var r: tRect; var W, Columns, rows: integer; Alist: pSelectCollection);
end;
{tPickDialog is the first "useful" object. Pass a tRect, a width, number of
columns and rows, and a TSelectCollection, and it will allow the user to
select up to <pick> items, marking the <selected> field of those items.}
pTextDialog = ^tTextDialog;
tTextDialog = object(tPickDialog)
IsValid : boolean;
List : pSelectCOllection;
constructor Init(var AList: pointer; X,Y: Integer; fn: string; name: string);
function LoadList(var fn, name, h: string; var temp: pCollection): boolean;
procedure InitList(h:string; t:pCollection); virtual;
function Valid(Command: Word): Boolean; virtual;
destructor Done; virtual;
end;
{tTextDialog is a little more complex. You pass an empty TSelectCollection,
the coords for where the list should appear, and it figures out how large
the dialog has to be. The TSelectCollection is built from a list (spec'ed
by <name>) in a text file <fn>.
The text file may have many lists in it, and follows this format:
NumberOfItems,ListName,NumberToPick
Item
Item
....
NumberOfItems,ListName,NumberToPick
....
tTextDialog returns ONLY the items that have been selected.}
function GetElement(S:String; N:byte): string;
function GetNumericElement(S:String; N:byte): longint;
implementation
function GetElement(S:String; N:byte): string;
var I,J,K: byte;
begin
I := 1; J := 0;
while(pos(',',S)>0) and (I<>N) do
begin
J := pos(',',s);
inc(I);
s[j] := ' ';
end;
If I<>N then GetElement := ''
else begin
inc(J);
K := pos(',',S);
If K = 0 then K := Length(S) + 1;
GetElement := copy(S,J,K-J);
end;
end;
function GetNumericElement(S:String; N:byte): longint;
var l:longint; code:integer;
begin
s := GetElement(S,N);
val(s,l,code);
GetNumericElement := l;
end;
{tSelectItem}
constructor tSelectItem.Init(S:String);
var w: byte;
begin
tObject.Init;
w := pos(' ',s);
if w = 0 then w := length(S);
Name := newStr(copy(S,1,w));
selected := false;
end;
destructor tSelectItem.Done;
begin
DisposeStr(Name);
end;
{tSelectCollection}
constructor tSelectCollection.Init(S:String);
begin
tCollection.Init(GetNumericElement(s,1),0);
Pick := GetNumericElement(s,3);
If Pick = 0 then Pick := 1;
end;
function tSelectCollection.NameAt(I: Integer): string;
begin
NameAt := tSelectItem(At(I)^).Name^;
end;
function tSelectCollection.Selected(I: Integer): boolean;
begin
Selected := tSelectItem(At(I)^).Selected;
end;
procedure tSelectCollection.ToggleAt(I: Integer);
begin
tSelectItem(At(I)^).Selected := not tSelectItem(At(I)^).Selected;
end;
procedure tSelectCollection.DropNotSelected;
var I: Integer;
begin
for I := Count-1 downto 0 do
if not tSelectItem(At(I)^).Selected
then Free(At(I));
end;
procedure tSelectCollection.NewItem(S:string);
begin
Insert(New(pSelectItem, init(S)));
end;
function tSelectCollection.LastSelectedItem: integer;
var I: integer;
begin
I := Count;
repeat dec(i) until (I=0) or Selected(I);
LastSelectedItem := I;
end;
function tSelectCollection.NumberSelected: integer;
var I, J: integer;
begin
J := 0;
for I := 0 to Count -1 do
if Selected(I) then inc(J);
NumberSelected := J;
end;
{tPICKLIST}
constructor tPickList.Init;
var I : integer;
p : pointer;
begin
tview.init(R);
EventMask := EventMask or evMouseMove;
Options := ofSelectable or ofTopSelect or ofPreProcess or ofCentered;
List := AList;
MaxItemLength := MIL;
NumCols := NC;
NumRows := NR;
picked := 0;
for I := 0 to List^.Count -1 do
if List^.Selected(I) then inc(picked);
end;
procedure tPickList.Draw;
var I, X, Y : byte;
s : string;
begin
X := 0; Y := 0;
for I := 0 to List^.Count-1 do
begin
If Y + 1 > NumRows then
begin
Y := 0;
Inc(X, MaxItemLength);
end;
{This code guarantees that s fills all space}
S := List^.NameAt(I);
while(Length(S)<MaxItemLength) do s := S + ' ';
if I = Highlight then writeStr(X, Y, s, 11)
else if List^.Selected(I) then writeStr(X,Y,S,3)
else writeStr(x,y,S,1);
Inc(y);
end;
S := '';
while(Length(S)<MaxItemLength) do s := S + ' ';
while(Y<=NumRows) do
begin
writestr(X,Y,S,1);
inc(y);
end;
end;
procedure tPickList.Choose(AnItem: Integer);
begin
If tSelectItem(List^.At(AnItem)^).Selected then dec(picked)
else inc(picked);
tSelectItem(List^.At(AnItem)^).Selected :=
not tSelectItem(List^.At(AnItem)^).Selected;
end;
procedure tPickList.HandleEvent;
var CoOrds: TPoint;
OH,I,J: integer;
r : tRect;
P : Pview;
s : string;
begin
tView.HandleEvent(Event);
If Event.What and (evBroadCast or evCommand) = 0 then
begin
Oh := Highlight;
if (event.What and evKeyboard <> 0) then
begin
case event.KeyCode of
kbDown : Inc(Highlight);
kbUp : Dec(Highlight);
kbRight: if numcols>1 then inc(Highlight,NumRows);
kbLeft : if numcols>1 then dec(Highlight,NumRows);
else
if Event.CharCode in [' ',#13] then
begin
If (Event.charCode = ' ') or not List^.Selected(Highlight)
then Choose(Highlight);
if Event.CharCode = #13 then picked := List^.pick;
end
else begin
I := Highlight; J := 0;
repeat
inc(I); Inc(J);
If I = List^.Count then I := 0;
If I < List^.Count then S := List^.NameAt(I);
un